import image
from media.display import *
from media.media import *

import os,utime,sys

# 显示模式选择：可以是 "VIRT"、"LCD" 或 "HDMI"
DISPLAY_MODE = "VIRT"

# 根据模式设置显示宽高
if DISPLAY_MODE == "VIRT":
    # 虚拟显示器模式
    DISPLAY_WIDTH = ALIGN_UP(1920, 16)
    DISPLAY_HEIGHT = 1080
elif DISPLAY_MODE == "LCD":
    # 3.1寸屏幕模式
    DISPLAY_WIDTH = 800
    DISPLAY_HEIGHT = 480
elif DISPLAY_MODE == "HDMI":
    # HDMI扩展板模式
    DISPLAY_WIDTH = 1920
    DISPLAY_HEIGHT = 1080
else:
    raise ValueError("未知的 DISPLAY_MODE，请选择 'VIRT', 'LCD' 或 'HDMI'")

def Display_Init():
    if DISPLAY_MODE == "VIRT":
        Display.init(Display.VIRT, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, fps=60)
    elif DISPLAY_MODE == "LCD":
        Display.init(Display.ST7701,width=DISPLAY_WIDTH,height=DISPLAY_HEIGHT,to_ide=True)
    elif DISPLAY_MODE == "HDMI":
        Display.init(Display.LT9611, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True)
    else:
        raise ValueError("未知的 DISPLAY_MODE，请选择 'VIRT', 'LCD' 或 'HDMI'")

def preprocess_image(img):
    # 使用中值滤波去噪
    img = img.median(1)

    # Canny 边缘检测
    img = img.find_edges(image.EDGE_CANNY, threshold=(50, 100))

    return img

def detect_circles(image):
    img=image
    circles=img.find_circles(threshold=1000, min_radius=50, max_radius=100)
    for circle in circles:
        print(circle)
        x=circle[0]
        y=circle[1]
        r=circle[2]
        print("圆心:({},{}),半径:{}".format(x,y,r))

        img.draw_circle(x,y,r,color=(128,128,128),thickness=2)

    return img

picture=image.Image("/data/Test01.bmp",format=image.ARGB8888).to_grayscale()
picture=preprocess_image(picture)

Display_Init()
MediaManager.init()

Display.show_image(picture)

picture=detect_circles(picture)
picture.save("/data/circle_detect01.bmp")

while True:

    Display.show_image(picture)


